<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Navigation extends Admin_Controller
{
	/**
	* called in module
	*/
	function dropdown()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_dropdown_model');
		
		// add to view
		$this->add('name', $CI->navigation_dropdown_model->name);
		$this->add('onchange', $CI->navigation_dropdown_model->onchange);
		$this->add('value', $CI->navigation_dropdown_model->value);
		$this->add('items', $CI->navigation_dropdown_model->items);
		
		// view
		$this->view('navigation_dropdown_view');
	}
		
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_list_model');
		$CI->load->module_model('navigation.navigation_model');
		
		// filter
		$CI->load->module_model('navigation.navigation_dropdown_model');
		$CI->navigation_dropdown_model->name = 'filter';
		$CI->navigation_dropdown_model->onchange = ' onchange="navigation.filter()"';
		$CI->navigation_dropdown_model->addOption(array(NAVIGATION_PAR_MENU_ID=>0, NAVIGATION_MENU_ID=>0, NAVIGATION_NAME=>'----- All Navigations -----'));
		if (isset($_POST['filter']))
			$CI->navigation_list_model->cache->add($CI->navigation_list_model->filter_cache_prefix, $_POST['filter']);
		$ts = $CI->navigation_list_model->cache->load($CI->navigation_list_model->filter_cache_prefix);
		$CI->navigation_dropdown_model->value = $ts;
		
		// sql
		if ($ts)
			$CI->db->where(NAVIGATION.'.'.NAVIGATION_PAR_MENU_ID, $ts);
		$CI->db->where(NAVIGATION.'.'.NAVIGATION_FRANCHISE_ID, $CI->navigation_list_model->admin_id());
		$q = $CI->db->get(NAVIGATION);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->navigation_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->navigation_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->from(NAVIGATION);
		$CI->db->select('*');
		if ($ts)
			$CI->db->where(NAVIGATION.'.'.NAVIGATION_PAR_MENU_ID, $ts);
		$CI->db->where(NAVIGATION.'.'.NAVIGATION_FRANCHISE_ID, $CI->navigation_list_model->admin_id());
		foreach ($CI->navigation_list_model->columns as $k=>$v)
		{
			if ($CI->navigation_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->navigation_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->navigation_list_model->start);
		$q = $CI->db->get();
		
		// gridview
		$this->add('items', $q->result());
		$this->add('start', $CI->navigation_list_model->start);
		foreach ($CI->navigation_list_model->columns as $k=>$v)
		{
			if ($CI->navigation_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->navigation_list_model->cache->load($k).'"');
			else
				$this->add('sort_'.$k,  '');
		}

		// message
		$this->add('msg', $CI->navigation_list_model->cache->load($CI->navigation_list_model->msg_cache_prefix));
		$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('navigation.toolbar', array('parent_id'=>0, 'start'=>$CI->navigation_list_model->start));
		$this->module_navigation('Navigation Management');
		
		// view
		$this->view('navigation_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_detail_model');
		$CI->load->module_model('navigation.navigation_model');
		
		// sql	
		$q = $CI->db->get_where(NAVIGATION, array(NAVIGATION_FRANCHISE_ID=>$CI->navigation_detail_model->admin_id(), NAVIGATION_MENU_ID=>$CI->navigation_detail_model->item));
		$entity = $q->row();
		
		// error
		$post = $CI->navigation_detail_model->cache->load('post');
		$CI->navigation_detail_model->cache->add('post', '');

		// add to view
		$this->add('name', $post?$post['name']:($entity?$entity->{NAVIGATION_NAME}:''));
		$this->add('topic', $post?$post['topic']:($entity?$entity->{NAVIGATION_TOPIC}:''));
		$this->add('ordering_text', $entity?$entity->{NAVIGATION_MENU_ID}:$CI->lang->line('priority_text'));
		
		// navigation dropdown
		$CI->load->module_model('navigation.navigation_dropdown_model');
		$CI->navigation_dropdown_model->name = 'par_menu_id';
		$CI->navigation_dropdown_model->value = $post?$post['par_menu_id']:($entity?$entity->{NAVIGATION_PAR_MENU_ID}:0);
		$CI->navigation_dropdown_model->addOption(array(NAVIGATION_PAR_MENU_ID=>0, NAVIGATION_MENU_ID=>0, NAVIGATION_NAME=>'----- Root -----'));
		
		// page dropdown
		$CI->load->module_model('page2.page2_dropdown_model');
		$CI->page2_dropdown_model->name = 'pid';
		$pid = $entity?$entity->{NAVIGATION_URL}:0;
		if ($pid)
			$pid = array_pop(explode('pid=', $pid));
		$CI->page2_dropdown_model->value = $post?$post['pid']:$pid;
		$CI->page2_dropdown_model->addOption(array(PAGE_PAGE_ID=>0, PAGE_TITLE=>'----- Select Page -----'));
		
		// add to view
		$this->add('start', $CI->navigation_detail_model->start);
		$this->add('item_id', $CI->navigation_detail_model->item);

		// message
		$this->add('msg', $CI->navigation_detail_model->cache->load($CI->navigation_detail_model->msg_cache_prefix));
		$CI->navigation_detail_model->cache->add($CI->navigation_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->navigation_detail_model->item)
			$this->module_navigation('Navigation : Edit');
		else
			$this->module_navigation('Navigation : New');
			
		// view
		$this->view('navigation_detail_view');
	}
}

?>